<?php
namespace App\Module\Admin\Controller;

use App\Common\Controller\AbstractController;
use App\Constants\Constant;
use App\Exception\AppException;
use App\Module\Admin\Request\Login\LoginRequest;
use App\Module\Admin\Service\AdminService;
use Hyperf\Di\Annotation\Inject;
use Illuminate\Contracts\Hashing\Hasher;
use Qbhy\HyperfAuth\AuthManager;

class LoginController extends AbstractController{

    /**
     * @Inject()
     * @var Hasher $hash
     */
    protected $hash;

    /**
     * @Inject()
     * @var AdminService $adminService
     */
    private $adminService;

    /**
     * @Inject()
     * @var AuthManager $authManager
     */
    private $authManager;

    //登陆
    public function account(LoginRequest $request){
        $username = $request->input('username');
        $password = $request->input('password');
        $user = $this->adminService->getByUsername($username);
        if (!$user) {
            throw new AppException("用户不存在");
        }
        if (!$this->hash->check($password, $user->password)) {
            throw new AppException("用户名或者密码错误");
        }
        return $this->response->json($this->authManager->guard(Constant::ADMIN)->login($user));
    }
}